package com.example.units;

public class ApiResponse<T> {
    private int code;       // HTTP状态码
    private boolean operSucc; // 操作是否成功
    private String msg;      // 消息
    private T data;         // 数据

    // 私有构造方法，强制使用静态工厂方法
    private ApiResponse(int code, boolean operSucc, String msg, T data) {
        this.code = code;
        this.operSucc = operSucc;
        this.msg = msg;
        this.data = data;
    }

    // ================== 成功响应 ==================
    public static <T> ApiResponse<T> success() {
        return new ApiResponse<>(200, true, "操作成功", null);
    }

    public static <T> ApiResponse<T> successData(T data) {
        return new ApiResponse<>(200, true, "操作成功", data);
    }

    public static <T> ApiResponse<T> success(String msg) {
        return new ApiResponse<>(200, true, msg, null);
    }

    public static <T> ApiResponse<T> success(T data, String msg) {
        return new ApiResponse<>(200, true, msg, data);
    }

    // ================== 失败响应 ==================
    public static <T> ApiResponse<T> failure(String msg) {
        return new ApiResponse<>(500, false, msg, null);
    }

    public static <T> ApiResponse<T> failure(int code, String msg) {
        return new ApiResponse<>(code, false, msg, null);
    }

    public static <T> ApiResponse<T> failure(String msg, T data) {
        return new ApiResponse<>(500, false, msg, data);
    }

    // ================== Getter/Setter ==================
    public int getCode() {
        return code;
    }

    public boolean isOperSucc() {
        return operSucc;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    @Override
    public String toString() {
        return "ApiResponse{" +
                "code=" + code +
                ", operSucc=" + operSucc +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}